package com.chixing.ih.module.programmer.service.Impl;

import com.chixing.ih.module.programmer.mapper.EvaluateMapper;
import com.chixing.ih.module.programmer.mapper.ImageMapper;
import com.chixing.ih.module.programmer.mapper.ProgrammerMapper;
import com.chixing.ih.module.programmer.pojo.Evaluate;
import com.chixing.ih.module.programmer.pojo.Programmer;
import com.chixing.ih.module.programmer.pojo.UserImage;
import com.chixing.ih.module.programmer.pojo.dto.EvaluateDTO;
import com.chixing.ih.module.programmer.service.IEvaluateService;
import com.chixing.ih.module.task.mapper.OrderMapper;
import com.chixing.ih.module.task.pojo.Order;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/3
 * Time: 23:15
 */
@Service
@Transactional
public class EvaluateServiceImpl implements IEvaluateService {

    @Autowired
    EvaluateMapper evaluateMapper;

    @Autowired
    ImageMapper imageMapper;

    @Autowired
    ProgrammerMapper programmerMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public List<EvaluateDTO> listAll(long userEvaluatedId) {
        List<EvaluateDTO> evaluates = evaluateMapper.listAll(userEvaluatedId);
        for (EvaluateDTO evaluateDTO : evaluates
        ) {
            UserImage oneByUserId = imageMapper.getOneByUserId(evaluateDTO.getUserEvaluateId());
            Programmer programmerMapperOne = programmerMapper.getOne(evaluateDTO.getUserEvaluateId());
            evaluateDTO.setImageUrl(oneByUserId.getImageUrl());
            evaluateDTO.setNickName(programmerMapperOne.getNickName());
            Integer skillScore = evaluateDTO.getSkillScore() == null ? 0 : evaluateDTO.getSkillScore();
            Integer attitudeScore = evaluateDTO.getAttitudeScore() == null ? 0 : evaluateDTO.getAttitudeScore();
            Integer punctualityScore = evaluateDTO.getPunctualityScore() == null ? 0 : evaluateDTO.getPunctualityScore();
            evaluateDTO.setAverageScore(Double.parseDouble(String.format("%.1f", (skillScore + attitudeScore + punctualityScore) / 3.0)));

        }
        return evaluates;
    }

    @Override
    public boolean insertOne(Evaluate evaluate) {
        evaluate.setCreateTime(new Date());
        evaluate.setUpdateTime(new Date());
        int record = evaluateMapper.insertOne(evaluate);
        if (record > 0) {
            Order order = new Order();
            order.setOrderId(evaluate.getOrderId());
            order.setPaymentStatus(2);
            order.setOrderEndTime(new Date());
            orderMapper.updateOne(order);
            return true;
        } else
            return false;
    }
}
